package com.das.mechanic_base.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3SelectColorAdapter;
import com.das.mechanic_base.bean.main.HomeNewColorBean;
import com.das.mechanic_base.utils.X3ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class X3HomeSelectColorDialog extends Dialog implements View.OnClickListener, X3SelectColorAdapter.OnItemClick {

    private Context mContext;

    private TextView tv_affirm;
    private RecyclerView rlv_color;
    private List<HomeNewColorBean> dataList=new ArrayList<>();
    private View rootView;
    private X3SelectColorAdapter colorAdapter;

    private HomeNewColorBean selectColor;

    public X3HomeSelectColorDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        this.mContext = context;
    }

    public X3HomeSelectColorDialog(@NonNull Context context, int themeResId, Context mContext) {
        super(context, themeResId);
        this.mContext = mContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootView = View.inflate(mContext, R.layout.x3_home_color_picker, null);
        setContentView(rootView);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        initView();
    }

    private void initView() {
        tv_affirm = findViewById(R.id.tv_affirm);
        rlv_color = findViewById(R.id.rlv_color);

        tv_affirm.setOnClickListener(this);

        rlv_color.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        colorAdapter = new X3SelectColorAdapter(mContext);
        rlv_color.setAdapter(colorAdapter);
        colorAdapter.setmOnItemClick(this);

    }

    public void changeData(String title, List<HomeNewColorBean> dataLists) {
        this.dataList = dataLists;
        if (colorAdapter != null) {
            colorAdapter.changeColor(dataList);
        }
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = null;
        view = findViewById(viewId);
        return (T) view;
    }

    @Override
    public void show() {
        super.show();
    }

    /**
     * 在dialog.show()之后调用 改变dialog的大小
     *
     * @param height 高度
     */
    public void setDialogWindowAttr(int height) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = X3ScreenUtils.getScreenWidth(mContext);
        lp.height = height;
        getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_affirm) {
            if (selectColor == null) {
                return;
            }
            if (iOnClick != null) {
                iOnClick.iOnClickSelect(selectColor);
            }
            dismiss();
        }
    }

    IOnClickCar iOnClick;

    public void setiOnClick(IOnClickCar iOnClick) {
        this.iOnClick = iOnClick;
    }

    @Override
    public void onClicks(HomeNewColorBean colorBean) {
        this.selectColor = colorBean;
    }

    public interface IOnClickCar {
        void iOnClickSelect(HomeNewColorBean data);
    }

    @Override
    public void dismiss() {
        this.selectColor = null;
        super.dismiss();
    }
}
